package com.zxl.grocery.microservice.crawler.common.base;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.ConsolePipeline;
import us.codecraft.webmagic.pipeline.Pipeline;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.List;

@Component
public abstract class AbstractPageProcessor  implements PageProcessor,AntAction {

    @Autowired
    private List<Pipeline> pipelines;

    private Logger logger = LoggerFactory.getLogger(getClass());

    private Site site = Site.me().setRetryTimes(0)
            .setSleepTime(10*1000)//十秒调用一次
            .setTimeOut(10000)//
            .setUserAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36");

    private String[] targetUrl;//目标爬虫路径

    private Spider spider= null ;

    public AbstractPageProcessor() {
        super();
        this.targetUrl=this.initTargetUrl();
        spider =  Spider.create(this).addUrl(this.targetUrl);//默认还是要创建一个
    }

    public abstract String[] initTargetUrl() ;//子类必须实现目标url

    @Override
    public Site getSite() {
        return site;
    }

    public void start() {
        if (Constant.SPIDER_STATUS_START.equals(this.status())) {//如果是在运行，就不再继续运行新的示例
            return;
        }
        logger.info("启动爬虫："+this.getClass()+";url:"+this.targetUrl.toString());
        spider =  Spider.create(this).addUrl(this.targetUrl);//真正启动时，重新创建
        spider.addPipeline(new ConsolePipeline())//添加控制台输出
        //.addPipeline(new JsonFilePipeline("D:\\webmagic\\data"))//添加json文件输出
        .thread(5);
        for (Pipeline pipeline : pipelines) {
            spider.addPipeline(pipeline);
        }
        spider.runAsync();
    }

    @Override
    public void restart() {
        logger.info("重启爬虫："+this.getClass()+";url:"+this.targetUrl.toString());
        this.stop();
        this.start();
        
    }

    @Override
    public void stop() {
        logger.info("关闭爬虫："+this.getClass()+";url:"+this.targetUrl.toString());
        spider.stop();
        
    }

    @Override
    public String status() {
        return spider.getStatus().toString();
    }

    public List<Pipeline> getPipelines() {
        return pipelines;
    }

    public void setPipelines(List<Pipeline> pipelines) {
        this.pipelines = pipelines;
    }
}
